package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.OfficeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/ordersetting")
//api/ordersetting/upload
public class OrderSettingControllers {
    @Autowired
    OrderSettingService orderSettingService;

//getOrderSettingByMonth
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        System.out.println("sdfhusf" + orderSetting.getOrderDate());
        orderSettingService.editNumberByDate(orderSetting);
        return new Result((true), MessageConst.GET_ORDERSETTING_SUCCESS);
    }

//getOrderSettingByMonth

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String year, String month) {
        System.out.println("时间：" + year + "," + month);
        List<Map> data = orderSettingService.getOrderSettingByMonth(Integer.parseInt(year), Integer.parseInt(month));
        return new Result(true, MessageConst.ORDER_SUCCESS, data);
    }
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) throws Exception {
        long size = excelFile.getSize();
        log.info("excelFile_size={}", size);
        if (size == 0) {
            return Result.error(null, "文件大小不能为0");
        }
        if (!OfficeUtils.checkExtension(excelFile)) {
            return Result.error(null, "请求文件类型错误:后缀名错误");
        }
        orderSettingService.upload(excelFile);
        return Result.success(null);
    }

}